/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.mine.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.mine.entity.Mine;
import com.thinkgem.jeesite.modules.mine.entity.Roadway;
import com.thinkgem.jeesite.modules.mine.service.MineService;
import com.thinkgem.jeesite.modules.mine.service.RoadwayService;

/**
 * 巷道信息Controller
 * @author developer
 * @version 2018-10-27
 */
@Controller
@RequestMapping(value = "${adminPath}/mine/roadway")
public class RoadwayController extends BaseController {

	@Autowired
	private RoadwayService roadwayService;
	@ModelAttribute
	public Roadway get(@RequestParam(required=false) String id) {
		Roadway entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = roadwayService.get(id);
		}
		if (entity == null){
			entity = new Roadway();
		}
		return entity;
	}
	
	@RequiresPermissions("mine:roadway:view")
	@RequestMapping(value = {"list", ""})
	public String list(Roadway roadway, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Roadway> page = roadwayService.findPage(new Page<Roadway>(request, response), roadway); 
		/*List<Roadway> list = page.getList();
		/*for (Roadway rw : list) {
			rw.setMineId(mineService.get(rw.getMineId()).getMinename());
		}*/
		model.addAttribute("page", page);
		return "modules/mine/roadwayList";
	}

	@RequiresPermissions("mine:roadway:view")
	@RequestMapping(value = "form")
	public String form(Roadway roadway, Model model,HttpServletRequest request,HttpServletResponse response) {
		//Page<Mine> page = mineService.findPage(new Page<Mine>(request, response), new Mine()); 
		//Map<String,String>map = new HashMap<String,String>();
		//List<Mine> list = page.getList();
		/*for (Mine mine : list) {
			map.put(mine.getId(), mine.getMinename());
		}*/
		//model.addAttribute("map", map);
		model.addAttribute("roadway", roadway);
		return "modules/mine/roadwayForm";
	}

	@RequiresPermissions("mine:roadway:edit")
	@RequestMapping(value = "save")
	public String save(Roadway roadway, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request,HttpServletResponse response) {
		if (!beanValidator(model, roadway)){
			return form(roadway, model,request,response);
		}
		roadwayService.save(roadway);
		addMessage(redirectAttributes, "保存巷道信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/roadway/?repage";
	}
	
	@RequiresPermissions("mine:roadway:edit")
	@RequestMapping(value = "delete")
	public String delete(Roadway roadway, RedirectAttributes redirectAttributes) {
		roadwayService.delete(roadway);
		addMessage(redirectAttributes, "删除巷道信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/roadway/?repage";
	}

}